import os
import utils
import imagesize
from tqdm import tqdm


path_xml = r'/media/fang/TOSHIBA EXT/Temp/Big_M1_save2'
merge_list = ['10', '11', '12', '13', '15']


for root, dirs, files in os.walk(path_xml):
    for file in tqdm(files):
        if file.split(".")[-1] == 'xml':
            infos = utils.ReadXml(root + '/' + file)
            # infos = sorted(infos, key=(lambda x: x[0]))

            infos_output = [root + '/' + file]
            # Rewrite = False

            # s = infos[0][-1] + infos[1][-1]
            # if s in merge_list:
            #     # Rewrite = True
            #     info_new = [infos[0][0], min(infos[0][1], infos[1][1]), infos[1][2], max(infos[0][3], infos[1][3]), s]
            #     infos_output.append(info_new)
            #     if len(infos) == 3:
            #         infos_output.append(infos[-1])
            # Rewrite = True
            for info in infos:
                infos_output.append(info)
            if "RemoveLimit" in file.split("_")[-1]:
                label_bord = "RemoveLimit"
            else:
                label_bord = "Limit"
            label_limit_removelimit = [0, 0, 300, 300, label_bord]
            infos_output.append(label_limit_removelimit)
            # if Rewrite:
            w, h = imagesize.get(root + '/' + file.split(".")[0] + '.jpg')
            utils.WriteXml(infos_output, w, h)
